package card

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/card"
	cardReq "github.com/flipped-aurora/gin-vue-admin/server/model/card/request"
)

type CasCardsService struct{}

// CreateCasCards 创建casCards表记录
// Author [yourname](https://github.com/yourname)
func (casCardsService *CasCardsService) CreateCasCards(casCards *card.CasCards) (err error) {
	err = global.GVA_DB.Create(casCards).Error
	return err
}

// DeleteCasCards 删除casCards表记录
// Author [yourname](https://github.com/yourname)
func (casCardsService *CasCardsService) DeleteCasCards(ID string) (err error) {
	err = global.GVA_DB.Delete(&card.CasCards{}, "id = ?", ID).Error
	return err
}

// DeleteCasCardsByIds 批量删除casCards表记录
// Author [yourname](https://github.com/yourname)
func (casCardsService *CasCardsService) DeleteCasCardsByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]card.CasCards{}, "id in ?", IDs).Error
	return err
}

// UpdateCasCards 更新casCards表记录
// Author [yourname](https://github.com/yourname)
func (casCardsService *CasCardsService) UpdateCasCards(casCards card.CasCards) (err error) {
	err = global.GVA_DB.Model(&card.CasCards{}).Where("id = ?", casCards.ID).Updates(&casCards).Error
	return err
}

// GetCasCards 根据ID获取casCards表记录
// Author [yourname](https://github.com/yourname)
func (casCardsService *CasCardsService) GetCasCards(ID string) (casCards card.CasCards, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&casCards).Error
	return
}

// GetCasCardsInfoList 分页获取casCards表记录
// Author [yourname](https://github.com/yourname)
func (casCardsService *CasCardsService) GetCasCardsInfoList(info cardReq.CasCardsSearch) (list []card.CasCards, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&card.CasCards{})
	var casCardss []card.CasCards
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.Type != nil && *info.Type != "" {
		db = db.Where("type = ?", *info.Type)
	}
	if info.CardId != nil && *info.CardId != "" {
		db = db.Where("card_id LIKE ?", "%"+*info.CardId+"%")
	}
	if info.Name != nil && *info.Name != "" {
		db = db.Where("name LIKE ?", "%"+*info.Name+"%")
	}
	if info.IdNumber != nil && *info.IdNumber != "" {
		db = db.Where("id_number = ?", *info.IdNumber)
	}
	if info.StartOpenAt != nil {
		db = db.Where("open_at >= ", info.StartOpenAt)
	}
	if info.EndOpenAt != nil {
		db = db.Where("open_at <= ", info.EndOpenAt)
	}
	if info.OpenIn != nil && *info.OpenIn != "" {
		db = db.Where("open_in = ?", *info.OpenIn)
	}
	if info.TownCode != nil && *info.TownCode != "" {
		db = db.Where("town_code = ?", *info.TownCode)
	}
	if info.FOwnerId != nil {
		db = db.Where("f_owner_id = ?", *info.FOwnerId)
	}
	if info.State != nil && *info.State != "" {
		db = db.Where("state = ?", *info.State)
	}
	if info.Smite != nil && *info.Smite != "" {
		db = db.Where("smite = ?", *info.Smite)
	}
	if info.StartIssueAt != nil {
		db = db.Where("issue_at >= ?", info.StartIssueAt)
	}
	if info.EndIssueAt != nil {
		db = db.Where("issue_at <= ?", info.EndIssueAt)
	}

	err = db.Count(&total).Error
	if err != nil {
		return
	}

	var OrderStr string
	if _, ok := orderMap[info.Sort]; ok {
		OrderStr = info.Sort
		if info.Order == "desc" {
			OrderStr = OrderStr + " desc"
		}
		db = db.Order(OrderStr)
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&casCardss).Error
	return casCardss, total, err
}
func (casCardsService *CasCardsService) GetCasCardsDataSource() (res map[string][]map[string]any, err error) {
	res = make(map[string][]map[string]any)

	fOwnerId := make([]map[string]any, 0)

	global.GVA_DB.Table("sys_users").Where("deleted_at IS NULL").Select("nick_name as label,id as value").Scan(&fOwnerId)
	res["fOwnerId"] = fOwnerId
	return
}
func (casCardsService *CasCardsService) GetCasCardsPublic() {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}

var orderMap = map[string]struct{}{
	"issue_at": {},
}
